package leetcode;

/**
 * Created by baoyunfeng on 2019/8/5.
 */
public class Median_of_Two_Sorted_Arrays {

    public static void main(String args[]) {
        System.out.print(findMedianSortedArrays(new int[]{3}, new int[]{-1,-2}));
    }

    public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int[] numsAll = new int[nums1.length + nums2.length];

        int index1 = 0;
        int index2 = 0;
        int length = numsAll.length;
        for (int i = 0; i < length; i++) {
            if (index1 >= nums1.length) {
                numsAll[i] = nums2[index2];
                index2++;
            } else if (index2 >= nums2.length) {
                numsAll[i] = nums1[index1];
                index1++;
            } else {
                if (nums1[index1] < nums2[index2]) {
                    numsAll[i] = nums1[index1];
                    index1++;
                } else {
                    numsAll[i] = nums2[index2];
                    index2++;
                }
            }
        }
        if (length % 2 == 0) {
            return ((double) numsAll[length / 2 - 1] + numsAll[(length / 2)]) / 2;
        } else {
            return numsAll[length / 2];
        }
    }
}
